import sys

import uiautomator2


def login(d, phone: str):
    """判断登录页面是否存在

    :param d: Uiautomator2
    :param phone: int
    :return: bool
    """
    # d.set_new_command_timeout(300)  # change to 5 minutes, unit seconds
    d.implicitly_wait(3.0)
    if not d(text="登录/注册").exists:
        print("账号已经登录了，请退出重试！！")
        return False

    print('正常登录')

    # 勾选 弹窗同意
    if d(text='同意').exists:
        d(text='同意').click()

    # 输入手机
    d(resourceId='input_phone').set_text(phone)
    # 输入验证码
    d(resourceId='input_vcode').set_text(666888)
    # 点击同意协议
    d(className='android.widget.CheckBox').click_exists(timeout=1)

    # 点击登录
    d(resourceId='login_btn').click()
    return True


def logout(d: uiautomator2.Device):
    """登出

    """
    d.click(0.734, 0.306)
